import tkinter as tk
from tkinter import messagebox
from src.core.item_manager import create_item

def show_creation_screen(parent, items):
    """Show character creation screen"""
    # Clear current interface
    for widget in parent.winfo_children():
        widget.destroy()
    
    # Title
    title = tk.Label(parent, text="➕ Create New Character", font=("Arial", 20))
    title.pack(pady=20)
    
    # Input frame
    input_frame = tk.Frame(parent)
    input_frame.pack(pady=20)
    
    # Name input
    tk.Label(input_frame, text="Character Name:", font=("Arial", 12)).grid(row=0, column=0, sticky="e", padx=10, pady=10)
    name_entry = tk.Entry(input_frame, font=("Arial", 12), width=20)
    name_entry.grid(row=0, column=1, padx=10, pady=10)
    
    # Class input
    tk.Label(input_frame, text="Class Type:", font=("Arial", 12)).grid(row=1, column=0, sticky="e", padx=10, pady=10)
    type_entry = tk.Entry(input_frame, font=("Arial", 12), width=20)
    type_entry.grid(row=1, column=1, padx=10, pady=10)
    
    # Level input
    tk.Label(input_frame, text="Level:", font=("Arial", 12)).grid(row=2, column=0, sticky="e", padx=10, pady=10)
    level_entry = tk.Entry(input_frame, font=("Arial", 12), width=20)
    level_entry.grid(row=2, column=1, padx=10, pady=10)
    
    def create_character():
        """Create character button click event"""
        name = name_entry.get().strip()
        char_type = type_entry.get().strip()
        level_str = level_entry.get().strip()
        
        # Input validation
        if not name or not char_type or not level_str:
            messagebox.showerror("Input Error", "Please fill all fields!")
            return
        
        try:
            level = int(level_str)
            if level < 1:
                raise ValueError("Level must be greater than 0")
        except ValueError:
            messagebox.showerror("Input Error", "Level must be a positive integer!")
            return
        
        # Check character limit
        if len(items) >= 4:
            messagebox.showerror("Operation Failed", "Maximum 4 characters allowed!")
            return
        
        # Create character
        create_item(items, name, char_type, level)
        messagebox.showinfo("Success", f"Character {name} created successfully!")
        
        # Return to selection screen
        from .selection_screen import show_selection_screen
        show_selection_screen(parent, items)
    
    # Button frame
    button_frame = tk.Frame(parent)
    button_frame.pack(pady=20)
    
    # Create button
    create_btn = tk.Button(button_frame, text="Create Character", command=create_character, 
                          bg="#2980b9", fg="white", font=("Arial", 12), padx=20)
    create_btn.pack(side=tk.LEFT, padx=10)
    
    # Back button
    def back_to_selection():
        from .selection_screen import show_selection_screen
        show_selection_screen(parent, items)
    
    back_btn = tk.Button(button_frame, text="Back", command=back_to_selection, 
                        font=("Arial", 12), padx=20)
    back_btn.pack(side=tk.LEFT, padx=10)